﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BasketStats.Core.Stats;
using BasketStats.Core.Team;
using BasketStats.Core.Game;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

namespace BasketStats
{
    public partial class MainForm : Form
    {
        private Game m_game;
        private Roster m_roster;

        public MainForm()
        {
            InitializeComponent();
        }

        public override void Refresh()
        {
            lblTotalPoints.Text = m_game.Team.SelectMany(x => x.CurrentStatistics.Shots).Where(x => x.Scored).Sum(x => (int)x.Type).ToString();
            base.Refresh();
        }

        private void mnuFileQuit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void fileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            mnuFileStartNewGame.Enabled = m_roster != null;
        }

        private void mnuFileOpenRoster_Click(object sender, EventArgs e)
        {
            using (var ofd = new OpenFileDialog())
            {
                ofd.Filter = "Roster File | *.roster";
                ofd.Title = "Open roster...";
                ofd.CheckFileExists = true;
                ofd.CheckPathExists = true;
                if (ofd.ShowDialog() == DialogResult.OK && File.Exists(ofd.FileName))
                {
                    using (var reader = new XmlTextReader(ofd.FileName))
                    {
                        reader.Read();
                        m_roster = new XmlSerializer(typeof(Roster)).Deserialize(reader) as Roster;
                        reader.Close();
                    }
                }
            }
        }

        private void mnuFileStartNewGame_Click(object sender, EventArgs e)
        {
            if (m_roster != null)
            {
                var selectorFrm = new GameRosterSelectorForm();
                selectorFrm.Roster = m_roster; //setta la property (setter..BAU!!)

                if (selectorFrm.ShowDialog() == DialogResult.OK)
                {
                    var roster = selectorFrm.Selected.Select(p => new Player { Info = p });
                    //var roster = m_roster.Players.Select(p => new Player { Info = p }).ToList();
                    var courtPlayers = roster.Take(5);
                    var benchPlayers = roster.Except(courtPlayers);
                    m_game = new Game(roster);

                    foreach (var cp in courtPlayers)
                    {
                        cp.CurrentStatistics.Shots.CollectionChanged += (o, ev) => Refresh();
                        cp.CurrentStatistics.GameStarted = true;
                        pnlCourt.Controls.Add(new PlayingPlayerView { Player = cp });
                    }

                    foreach (var cp in benchPlayers)
                    {
                        cp.CurrentStatistics.Shots.CollectionChanged += (o, ev) => Refresh();
                        cp.CurrentStatistics.GameStarted = false;
                        pnlBench.Controls.Add(new BenchPlayerThumbnailView { Player = cp });
                    }

                    m_game.StartGame();
                }
            }
        }
    }
}
